{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Curve Serialization"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "The classes `NelsonSiegelCurve` and `NelsonSiegelSvenssonCurve` in the `nelson_siegel_svensson` Python package are `dataclasses` in terms of the curve parameters. They can simply be converted to and from dictionaries which in turn are simple enough for JSON serialization."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "from nelson_siegel_svensson import NelsonSiegelSvenssonCurve\n",
    "import json"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "NelsonSiegelSvenssonCurve(beta0=0.028, beta1=-0.03, beta2=-0.04, beta3=-0.015, tau1=1.1, tau2=4.0)"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "y1 = NelsonSiegelSvenssonCurve(0.028, -0.03, -0.04, -0.015, 1.1, 4.0)\n",
    "y1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'beta0': 0.028,\n",
       " 'beta1': -0.03,\n",
       " 'beta2': -0.04,\n",
       " 'beta3': -0.015,\n",
       " 'tau1': 1.1,\n",
       " 'tau2': 4.0}"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# curves can easily be converted to Python dictionaries of the curve parameters\n",
    "vars(y1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{\n",
      "  \"beta0\": 0.028,\n",
      "  \"beta1\": -0.03,\n",
      "  \"beta2\": -0.04,\n",
      "  \"beta3\": -0.015,\n",
      "  \"tau1\": 1.1,\n",
      "  \"tau2\": 4.0\n",
      "}\n"
     ]
    }
   ],
   "source": [
    "# serialize parameters to JSON\n",
    "s = json.dumps(vars(y1), indent=2)\n",
    "print(s)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "NelsonSiegelSvenssonCurve(beta0=0.028, beta1=-0.03, beta2=-0.04, beta3=-0.015, tau1=1.1, tau2=4.0)"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# deserialize parameters from JSON and pass them to the constructor\n",
    "y2 = NelsonSiegelSvenssonCurve(**json.loads(s))\n",
    "y2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [],
   "source": [
    "assert y1 == y2 # deserialized curve is equal to original one...\n",
    "assert y1 is not y2 # ...but there is no trickery, it is a separate object"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.2"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
